/*
 *  LLDateTime.cpp
 *
 *
 *  Created by Alexander Trutchenko on 11/02/12.
 *  Copyright 2012 Free Team. All rights reserved.
 *
 */

#include "lid_lib/src/time_managment/LLTimeManager.h"

#include "lid_lib/src/data_managment/LLNotifier.h"

#include "lid_lib/src/config/LLConfig.h"
#include "lid_lib/src/config/LLKeys.h"

#include <FBase.h>
#include <FLocales.h>


namespace lidlib {

////////////////////////////////////////////////////////
// utility functions

static ELLWeekDay getWeekDayFromTime(const Osp::Base::DateTime& utcTime)
{
	Osp::Locales::LocaleManager localeManager;
	localeManager.Construct();
	Osp::Locales::TimeZone timeZone = localeManager.GetSystemTimeZone();
	Osp::Locales::Calendar* pCalendar =	Osp::Locales::Calendar::CreateInstanceN(timeZone, Osp::Locales::CALENDAR_GREGORIAN);
	pCalendar->SetTime(utcTime);
	pCalendar->GetTimeField(Osp::Locales::TIME_FIELD_DAY_OF_WEEK);

	switch (pCalendar->GetTimeField(Osp::Locales::TIME_FIELD_DAY_OF_WEEK) - 1)
	{
	case 0: return ell_wd_sunday;
	case 1: return ell_wd_monday;
	case 2: return ell_wd_tuesday;
	case 3: return ell_wd_wednesday;
	case 4: return ell_wd_thursday;
	case 5: return ell_wd_friday;
	case 6: return ell_wd_saturday;
	default: break;
	}
	return ell_wd_invalid;
}

////////////////////////////////////////////////////////
//
/** LLTimeManager
 *
 */
////////////////////////////////////////////////////////

unsigned int LLTimeManager::getSeconds()
{
	Osp::Base::DateTime utcTime;
	Osp::System::SystemTime::GetCurrentTime( Osp::System::WALL_TIME,utcTime);
	return utcTime.GetSecond();
}

unsigned int LLTimeManager::getMinutes()
{
	Osp::Base::DateTime utcTime;
	Osp::System::SystemTime::GetCurrentTime( Osp::System::WALL_TIME,utcTime);
	return utcTime.GetMinute();
}

unsigned int LLTimeManager::getHours()
{
	Osp::Base::DateTime utcTime;
	Osp::System::SystemTime::GetCurrentTime( Osp::System::WALL_TIME,utcTime);
	return utcTime.GetHour();
}

unsigned int LLTimeManager::getMonthDay()
{
	Osp::Base::DateTime utcTime;
	Osp::System::SystemTime::GetCurrentTime( Osp::System::WALL_TIME,utcTime);
	return utcTime.GetDay();
}

unsigned int LLTimeManager::getMonth()
{
	Osp::Base::DateTime utcTime;
	Osp::System::SystemTime::GetCurrentTime( Osp::System::WALL_TIME,utcTime);
	return utcTime.GetMonth();
}

unsigned int LLTimeManager::getYear()
{
	Osp::Base::DateTime utcTime;
	Osp::System::SystemTime::GetCurrentTime( Osp::System::WALL_TIME,utcTime);
	return utcTime.GetYear();
}

ELLWeekDay LLTimeManager::getWeekDay()
{
	Osp::Base::DateTime utcTime;
	Osp::System::SystemTime::GetCurrentTime( Osp::System::WALL_TIME,utcTime);
	return getWeekDayFromTime(utcTime);
}

////////////////////////////////////////////////////////
// Private interface

void LLTimeManager::update(float dt)
{
	Osp::Base::DateTime utcTime;
	Osp::System::SystemTime::GetCurrentTime( Osp::System::WALL_TIME,utcTime);

	compareTime(utcTime.GetSecond(),
				utcTime.GetMinute(),
				utcTime.GetHour(),
				utcTime.GetDay(),
				utcTime.GetMonth(),
				utcTime.GetYear(),
				getWeekDayFromTime(utcTime));
}

} // namespace lidlib
